package com.aijiang.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.aijiang.service.HtmlTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class HtmlToolImpl implements HtmlTool {

	@Override
	public String getContent(String url) {
		String content = null;
		try {

			// Document doc = Jsoup.connect("http://example.com").cookie("auth",
			// "token").post();
			// Document doc = Jsoup.connect(url).userAgent("Mozilla").data("name",
			// "jsoup").get();
			Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
			content = doc.html();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}

	@Override
	public List<String> getAllLinks(Document doc) {
		List<String> links = new ArrayList<>();
		List<Element> elements = doc.getElementsByTag("a");
		for (Element element : elements) {
			String linkHref = element.attr("href");
			String linkText = element.text();
			// System.out.println(linkHref + ":" + linkText);
			links.add(linkHref + ":" + linkText);
		}
		return links;
	}

	@Override
	public List<String> getAllLinksByClass(Document doc, String cssname) {
		return null;
	}

	@Override
	public Document getDocument(String url) {
		try {
			Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
			return doc;

		} catch (Exception e) {
			System.err.println(e);
			return null;
		}

	}

	public static void main(String[] args) {
		HtmlTool htmlTool = new HtmlToolImpl();
		// String url = "https://www.bilibili.com/";
		String url = "https://boards.4chan.org/ck/catalog";
		// System.out.println(htmlTool.getContent(url));
		// System.out.println(htmlTool.getAllLinks(htmlTool.getDocument(url)));
		Document doc = htmlTool.getDocument(url);
		List<Element> elements = doc.head().getElementsByTag("script");
		for (Element var : elements) {
			System.out.println(var.data());
		}
	}
}
